//
//  DHEvaluateController.m
//  精品酒店
//
//  Created by DIANGHONG on 2018/5/11.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "DHEvaluateController.h"
#import "DHEvaluateCell.h"
#import "DHEvaFilterView.h"
NSString *evaCellID = @"evaCellID";
@interface DHEvaluateController ()<UITableViewDataSource,DHFilterViewDelegate,UITableViewDelegate>
@property(nonatomic ,strong)DHNavbarView *navbar;
@property(nonatomic ,strong)UITableView *evaListView; // 评价表格视图
@property(nonatomic ,assign)BOOL hasMore;
@property(nonatomic ,assign)int page;
@property(nonatomic ,strong)NSArray<DHEvaluateModel *> *evaList;
@property(nonatomic ,assign)int gCount;
@property(nonatomic ,assign)int mCount;
@property(nonatomic ,assign)int bCount;
@property(nonatomic ,copy)NSString *evaluate_type;
@property(nonatomic ,strong)UIButton *currentBtn; // 记录当前选中按钮
@property(nonatomic ,strong)DHEvaFilterView *filterView;
@end

@implementation DHEvaluateController

- (DHEvaFilterView *)filterView{
    if (!_filterView) {
        _filterView = [DHEvaFilterView filterViewWithFrame:CGRectMake(0, CGRectGetMaxY(self.navbar.frame),
                                                                   kScreenWidth, 44)];
    }
    return _filterView;
}
- (void)setEvaList:(NSArray<DHEvaluateModel *> *)evaList
{
    _evaList = evaList;
    
    self.filterView.gCount = self.gCount;
    self.filterView.mCount = self.mCount;
    self.filterView.bCount = self.bCount;
    [self.evaListView reloadData];
}

- (UITableView *)evaListView{
    if (!_evaListView) {
        CGRect rect = self.type ? self.navigationController.navigationBar.frame : self.filterView.frame;
        _evaListView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(rect)
                                                                     , kScreenWidth
                                                                     ,kScreenHeight - CGRectGetMaxY(rect))];
        _evaListView.dataSource = self;
        _evaListView.delegate = self;
        [_evaListView registerNib:[UINib nibWithNibName:@"DHEvaluateCell" bundle:nil] forCellReuseIdentifier:evaCellID];
       // _evaListView.rowHeight = 200;
        
    }
    return _evaListView;
}
// lazy navbar
- (DHNavbarView *)navbar{
    if (!_navbar)
    {
        CGRect frame;
        frame = KIsiPhoneX ? CGRectMake(0, 0, kScreenWidth, 88) : CGRectMake(0, 0, kScreenWidth, 64);
        _navbar = [DHNavbarView navbarViewWithFrame:frame title:@"全部评价" isHiddenReturn:NO];
    }
    return _navbar;
}

- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self setupUI];
    
//    if (self.isAllUserEva) {
//        [self loadAllUserEvaList];
//    }

    
    self.page = 1;
    //[DHNetworkManager.shareManager requestEvaListWithParam:dictM];
   // [self loadEvaListWithPullup:NO];
    [self.evaListView.mj_header beginRefreshing];
}
- (void)loadEvaListWithPullup:(BOOL)pullup
{
    if (pullup)
    {
        if (!self.evaList) {return;}
        if (self.hasMore) {
            self.page++;
        }else{
            [self.evaListView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
    }
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    //NSString *url = @"";
    NSString *url = @"http://jpjd.cnaite.cn/mobile/index.php?act=cleaning_info&op=getEvaluateList";
    dictM[@"key"] = [DHNetworkManager shareManager].userAccount.key;
    dictM[@"type"] = self.type ? self.type : @"2";
    dictM[@"curpage"] = @(self.page);
    dictM[@"id"] = self.model.address_id ? self.model.address_id : DHNetworkManager.shareManager.userAccount.userid;
    dictM[@"evaluate_type"] = self.evaluate_type;
    [DHNetworkManager.shareManager requestWithMethod:MethodGet UrlString:url params:dictM.copy completion:^(id json, BOOL isSuccess) {
        
        if (!isSuccess) {
            [SVProgressHUD showErrorWithStatus:@"请检查网络"];
            [self.evaListView.mj_footer endRefreshing];
            [self.evaListView.mj_header endRefreshing];

            return ;
        }
        if (pullup) {
            
            self.evaList = [self.evaList arrayByAddingObjectsFromArray:[NSArray yy_modelArrayWithClass:[DHEvaluateModel class] json:json[@"datas"][@"list"]]];
            [self.evaListView.mj_footer endRefreshing];

        }else{
            self.evaList = [NSArray yy_modelArrayWithClass:[DHEvaluateModel class] json:json[@"datas"][@"list"]];//json[@"datas"][@"list"];
            self.hasMore = [json[@"hasmore"] boolValue];
            if (!self.type) {
                self.filterView.gCount = [json[@"datas"][@"good_count"]intValue];
                self.filterView.mCount = [json[@"datas"][@"medium_count"]intValue];
                self.filterView.bCount = [json[@"datas"][@"bag_count"]intValue];
            }
            [self.evaListView.mj_header endRefreshing];

        }
        NSLog(@"%@",self.evaList);
    }];

}
#pragma mark: - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.evaList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DHEvaluateCell *cell = [tableView dequeueReusableCellWithIdentifier:evaCellID forIndexPath:indexPath];
    cell.model = self.evaList[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    return [[UITableViewCell alloc] init];
}
#pragma mark: - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat rowH = self.evaList[indexPath.row].imageList ? 200 : 200 - 63.0;
    return rowH;
}

#pragma mark: - DHFilterViewDelegate
- (void)filterView:(UIView *)view DidSelectedButton:(UIButton *)sender
{
    self.currentBtn.selected = NO;
    sender.selected = YES;
    self.currentBtn = sender;
    
    switch (sender.tag) {
        case 0: // 全部评价
            self.evaluate_type = nil;
            self.evaList = nil;
            [self.evaListView.mj_header beginRefreshing];
            break;
        case 1: // 好评
            self.evaluate_type = @"good";
            self.evaList = nil;
            [self.evaListView.mj_header beginRefreshing];
            break;
        case 2:
            self.evaluate_type = @"medium";
            self.evaList = nil;
            [self.evaListView.mj_header beginRefreshing];
            break;
        case 3:
            self.evaluate_type = @"bad";
            self.evaList = nil;
            [self.evaListView.mj_header beginRefreshing];
        case 4:
            self.evaluate_type = @"image";
            self.evaList = nil;
            [self.evaListView.mj_header beginRefreshing];
            break;
    }
}

#pragma mark: - 设置界面
- (void)setupUI
{
    __weak typeof(self) WeakSelf = self;
    self.view.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:self.navbar];
    [self.view addSubview:self.evaListView];
    if (!self.type) {
        [self.view addSubview:self.filterView];
    }
    
    self.filterView.delegate = self;
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadEvaListWithPullup:NO];
    }];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadEvaListWithPullup:YES];
    }];
    self.evaListView.mj_header = header;
    self.evaListView.mj_footer = footer;
    self.navbar.popButtonClickBlock =
    ^{
        [WeakSelf.navigationController popViewControllerAnimated:true];
    };
}

@end
